/**
 * Copyright (c) 2018-2022, NXOS Development Team
 * SPDX-License-Identifier: Apache-2.0
 *
 * Contains: IPC
 *
 * Change Logs:
 * Date           Author            Notes
 * 2023-07-23     kangjinci           Init
 */

#pragma once

#include <netsocket.h>

void Do_NET_Init(NX_Size buf_size);

typedef enum NET_ReqType
{
    NET_REQ_INVALID,
    NET_REQ_SOCKET,
    NET_REQ_CLOSE,
    NET_REQ_BIND,
    NET_REQ_LISTEN,
    NET_REQ_ACCEPT,
    NET_REQ_CONNECT,
    NET_REQ_SEND,
    NET_REQ_RECV,
    NET_REQ_SENDTO,
    NET_REQ_RECVFROM,
    NET_REQ_SHUTDOWN,
    NET_REQ_CONTROL,
    NET_REQ_SETSOCKOPT,
    NET_REQ_GETSOCKOPT,
} NET_ReqType;

typedef struct NET_IpcReq
{
    NET_ReqType type;
    union
    {
        struct NET_SocketArgs
        {
            NX_Error err;
            int domain;
            int type;
            int protocol;
        } net_socket_args;
        struct NET_CloseArgs
        {
            int socket;
        } net_close_args;
        struct NET_BindArgs
        {
            int socket;
            struct sockaddr addr;
            socklen_t addrlen;
        } net_bind_args;
        struct NET_ListenArgs
        {
            int socket;
            int backlog;
        } net_listen_args;
        struct NET_AcceptArgs
        {
            NX_Error err;
            int socket;
            struct sockaddr addr;
            socklen_t addrlen;
        } net_accept_args;
        struct NET_ConnectArgs
        {
            int socket;
            struct sockaddr addr;
            socklen_t addrlen;
        } net_connect_args;
        struct NET_SendArgs
        {
            NX_Error err;
            int socket;
            int len;
            int flags;
            char buf[0];
        } net_send_args;
        struct NET_RecvArgs
        {
            NX_Error err;
            int socket;
            int len;
            int flags;
            char buf[0];
        } net_recv_args;
        struct NET_ShutdownArgs
        {
            int socket;
            int how;
        } net_shutdown_args;
        struct NET_ControlArgs
        {
            int socket;
            long cmd;
            unsigned long argp;
        } net_control_args;
        struct NET_SendtoArgs
        {
            NX_Error err;
            int socket;
            unsigned long size;
            int flags;
            struct sockaddr to;
            socklen_t tolen;
            char buf[0];
        } net_sendto_args;
        struct NET_RecvfromArgs
        {
            NX_Error err;
            int socket;
            unsigned long size;
            int flags;
            struct sockaddr from;
            socklen_t fromlen;
            char buf[0];
        } net_recvfrom_args;
        struct NET_SetsockoptArgs
        {
            int socket;
            int level;
            int optname;
            socklen_t optlen;
            char optval[0];
        } net_setsockopt_args;
        struct NET_GetsockoptArgs
        {
            int socket;
            int level;
            int optname;
            socklen_t optlen;
            char optval[0];
        } net_getsockopt_args;  
    };

} NET_IpcReq;

#define GET_NET_REQUEST_SIZE(tag) \
    (NX_OFFSET_OF_STRUCT(NET_IpcReq, net_##tag##_args) + sizeof(((NET_IpcReq *)0)->net_##tag##_args))
